package agents.machine;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.text.DefaultCaret;
import org.joda.time.DateTime;

/**
 * This class represents the MachineAgent's Graphic User Interface.
 * @author Group 6
 */
public class MachineAgentGUI extends javax.swing.JFrame {

    private MachineAgent machineAgent;
    private String machineLocalName;
    private String machineWorkType;
    private String machineState;
    private String machineFSMState;
    private String wpID;
    private String wpOrderID;
    
    public MachineAgentGUI(MachineAgent machineAgent) {
        initComponents();
        // Guarda referência para a classe MachineAgent
        this.machineAgent = machineAgent;
        // Define título da JFrame
        this.setTitle("Machine Starting!");
        // Define funcionamento do log
        DefaultCaret caret = (DefaultCaret) this.jTextArea1_machineLog.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        // Apresenta a JFrame
        this.showGui();
    }
    
    private void showGui() {
        pack();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int centerX = (int) screenSize.getWidth() / 2;
        int centerY = (int) screenSize.getHeight() / 2;
        setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);
        super.setVisible(true);
    }

    public void setMachineLocalName(String machineLocalName) {
        this.machineLocalName = machineLocalName;
    }

    public void setMachineWorkType(String machineWorkType) {
        this.machineWorkType = machineWorkType;
    }

    public void setMachineState(String machineState) {
        this.machineState = machineState;
    }

    public void setMachineFSMState(String machineFSMState) {
        this.machineFSMState = machineFSMState;
    }

    public void setWpID(String wpID) {
        this.wpID = wpID;
    }

    public void setWpOrderID(String wpOrderID) {
        this.wpOrderID = wpOrderID;
    }
    
    public void appendInfoToMachineLog(String newInfo) {
        DateTime now = new DateTime();
        String time = Integer.toString(now.getYear())
                + "/"
                + Integer.toString(now.getMonthOfYear())
                + "/"
                + Integer.toString(now.getDayOfMonth())
                + " - "
                + Integer.toString(now.getHourOfDay())
                + ":"
                + Integer.toString(now.getMinuteOfHour())
                + ":"
                + Integer.toString(now.getSecondOfMinute());
        String text = time + "\n\t" +  newInfo + "\n";
        this.jTextArea1_machineLog.append(text);
    }
    
    public void updateAllInfo() {
        this.setTitle(this.machineLocalName
                + " | "
                + this.machineWorkType
                + " Machine");
        this.jLabel_machineState.setText(this.machineState);
        this.jLabel3_machineFSMState.setText(this.machineFSMState);
        this.jLabel6_wpID.setText(this.wpID);
        this.jLabel6_wpOrderID.setText(this.wpOrderID);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel_machineState = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3_machineFSMState = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6_wpID = new javax.swing.JLabel();
        jLabel6_wpOrderID = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1_machineLog = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        jLabel1.setText("State:");

        jLabel_machineState.setText("N/A");

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        jLabel2.setText("FSM State:");

        jLabel3_machineFSMState.setText("N/A");

        jLabel3.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        jLabel3.setText("WorkPackage:");

        jLabel4.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        jLabel4.setText("ID:");

        jLabel5.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        jLabel5.setText("Order ID:");

        jLabel6_wpID.setText("N/A");

        jLabel6_wpOrderID.setText("N/A");

        jLabel6.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        jLabel6.setText("Log:");

        jTextArea1_machineLog.setEditable(false);
        jTextArea1_machineLog.setColumns(20);
        jTextArea1_machineLog.setRows(5);
        jScrollPane1.setViewportView(jTextArea1_machineLog);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel2))
                        .add(49, 49, 49)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel3_machineFSMState)
                            .add(jLabel_machineState)))
                    .add(jLabel3)
                    .add(jLabel6)
                    .add(layout.createSequentialGroup()
                        .add(128, 128, 128)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel4)
                            .add(jLabel5))
                        .add(29, 29, 29)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel6_wpOrderID)
                            .add(jLabel6_wpID))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .add(layout.createSequentialGroup()
                .add(24, 24, 24)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jLabel_machineState))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jLabel3_machineFSMState))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(jLabel6_wpID))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(jLabel6_wpOrderID))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel6)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        this.machineAgent.shutdownAgentAndGUI();
    }//GEN-LAST:event_formWindowClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel3_machineFSMState;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel6_wpID;
    private javax.swing.JLabel jLabel6_wpOrderID;
    private javax.swing.JLabel jLabel_machineState;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1_machineLog;
    // End of variables declaration//GEN-END:variables
}
